<template>
	<view>
		<view class="cu-modal drawer-modal justify-end" :class="modalNameDetail == 'DrawerModalR' ? 'show' : ''"
			@tap="hideModal">
		<!-- basis-xl zb -->
			<view class="cu-dialog basis-xl" @tap.stop="">
				<!-- <view class="cu-bar bg-white">
          <view class="action text-green" @tap="confirm">确定</view>
          <view class="action text-blue" @tap="hideModal">取消</view>
        </view> -->
				<view class="container">
					<view class="cu-modal-slot bg-white">
						<view class="cu-modal-header">明细筛选</view>
						<view class="cu-modal-search">
							<!-- <view class="cu-modal-search-title">物料名称</view> -->
							<view class="margin-top-sm">
								<u-input v-model="query.mname" :type="type" :border="border" placeholder="物料名称或规格型号"
									@input="input" clearable="true" />
							</view>
						</view>
					</view>
					<scroll-view class="scroll-region" scroll-y @scrolltolower="loadMore" :scrolltolower-enabled="true">
						<!-- 展示caiLiaoData的数据，以卡片的形式展示，每个卡片的左侧的第一项是一个复选框按钮 -->
						<view class="cu-list menu-avatar">
							<!-- class="cu-item" -->
							<view style="margin: 10px;padding: 10px;background: #fff;display: flex;justify-content: space-between; "   v-for="(item, index) in list" :key="item.id">


								<view >
									<!-- <view class="text-black flex font-weight-bold">
										<view>
											<view
												style=" display: inline-block;padding: 5px;background: #3E97B0;color: #fff;width: 40px;text-align: center;font-style: italic;font-weight: bold"
												class="margin-right-sm">
												{{ index + 1 }}
											</view>{{ item.mname }}
										</view>
									</view> -->
									<view class="text-black flex font-weight-bold">
										<!-- <view class="flex-sub">材料名称：</view> -->
										<view>{{ item.mname || ""  }}</view>
									</view>
									<view class="text-black flex">
										<!-- <view class="flex-sub">规格型号：</view> -->
										<view>{{ item.mstd || ""  }}</view>
									</view>
									<view class="text-black flex">
										<view class="flex-sub">数量：</view>
										<view>
											<view
												style="display: inline-block;width: 70px;border-bottom: 1px solid #eee;text-align: center;">
												{{ item.num || "0" }}
											</view>
											{{ item.unit || "" }}
										</view>
									</view>
									<view class="text-black flex">
										<view class="flex-sub">已消耗数：</view>
										<view>
											<view
												style="display: inline-block;width: 70px;border-bottom: 1px solid #eee;text-align: center;">
												{{ item.sumXhQty || "0" }}
											</view>
											{{ item.unit || "" }}
										</view>
									</view>
									<view class="text-black flex">
										<view class="flex-sub">剩余数量：</view>
										<view>
											<view
												style="display: inline-block;width: 70px;border-bottom: 1px solid #eee;text-align: center;">
												{{ item.syQty || "0" }}
											</view>
											{{ item.unit || "" }}
										</view>

									</view>
									<!-- <view class="text-black flex">
										<view class="flex-sub">单位：</view>
										<view>{{ item.unit || "" }}</view>
									</view> -->
								</view>

								<view style="width: 50px;display: flex;justify-content: center;align-items: center;">
									<checkbox :checked="item.checked" @click="checkSelect(item)" />
								</view>
							</view>
						</view>
						<u-empty v-if="!list.length"></u-empty>
						<view v-if="list.length" class="loadmore">
							<u-loadmore :status="loadStatus"></u-loadmore>
						</view>
					</scroll-view>
				</view>

				<view class="cu-bar bg-white flex padding">
					<button class="cu-btn   lines-red lg  " @tap="hideModal">取消</button>
				<button class="cu-btn   bg-confirm lg margin-left" @tap="confirm">确定</button>
				</view>


			</view>
		</view>
	</view>
</template>
<script>
	export default {
		props: {
			modalNameDetail: {
				type: String,
				default: "DrawerModalR",
			},
			pcode: {
				type: String,
				default: "",
			},
		},
		data() {
			return {
				// modalName:'bottomModal',
				modalName: null,
				checkbox: [{
						value: "A",
						checked: true,
					},
					{
						value: "B",
						checked: true,
					},
					{
						value: "C",
						checked: false,
					},
				],
				loadStatus: "loadmore",
				list: [],
				border: true,
				checkedList: [],
				query: {
					bmain: 1,
					pcode: this.pcode,
					pageNo: 1,
					pageSize: 20,
				}
			};
		},
		created() {
			console.log(this.modalNameDetail, "modalNameDetail");

			this.loadData();
		},
		methods: {
			checkSelect(item) {
				item.checked = !item.checked;
				console.log(item.checked, item);
			},
			loadMore() {
				this.loadStatus = "loading";
				setTimeout(() => {
					this.query.pageNo += 1;
					this.loadData("add");
				}, 100);
			},
			loadData(type) {
				// this.query = {

				// };
				this.$u.api.zfgs.pmMaterialListData(this.query).then((res) => {
					if (res.list.length >= 0) {
						// this.xmList = res.list
						if (res.list.length < 20 || res.list.length == 0) {
							this.loadStatus = "nomore";
						}
						var data = res.list;
						//模拟收藏
						data.forEach((item, index) => {
							// 添加isStar 属性 随机2.5.8属性为true,其他为false
							item.checked = false;
						});
						console.log(this.loadStatus, "this.loadStatus");
						// if (this.loadStatus == "nomore") {
						// 	this.list = data;
						// } else {
							if (type == "add") {
								for (var i = 0; i < data.length; i++) {
									this.list.push(data[i]);
								}
							} else {
								this.list = data;
							}
						// }
					}
				});
			},
			showModal() {
				// this.modalName = 'bottomModal'
				this.$emit("modalNameDetailEmit", "DrawerModalR");
			},
			hideModal() {
				// this.modalName = null
				this.$emit("modalNameDetailEmit", null);
			},
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url,
				});
			},
			DateChange(e) {
				this.date = e.detail.value;
			},
			confirm() {
				this.$u.toast("确认成功");
				this.list.forEach((item) => {
					if (item.checked) {
						this.checkedList.push(item);
					}
				});
				if (this.checkedList.length == 0) {
					this.$u.toast("请选择材料");
					return;
				}
				this.$emit("checkedListEmit", this.checkedList);
				this.$emit("modalNameDetailEmit", "");
			},
			textareaAInput(e) {},
			input(e) {
				this.query.pageNo = 1;
				this.query.mname = e;
				this.loadData();
			},


		},
	};
</script>
<style scoped lang="scss">
	.zb {
		width: 100% !important;
	}
	.text-black{
		padding: 5px;
	}

	.font-weight-bold {
		font-weight: bold;
	}

	.flex-sub {
		flex: 0 0 auto;
		/* 设置固定的宽度，不允许伸缩 */
		text-align: left;
		/* 文本左对齐 */
		padding-right: 10rpx;
		/* 可以添加一些右边距以提高可读性 */
		width: 180rpx;
	}

	.cu-modal-slot {
		padding: 42rpx 32rpx 20rpx 32rpx;
		margin-bottom: 20rpx;
		position: relative;
	}

	.cu-modal-header {
		font-size: 38rpx;
		color: #3d3d3d;
		line-height: 40rpx;
		text-align: left;
		font-weight: 500;
	}

	.cu-modal-search-title {
		margin: 32rpx 0rpx 20rpx 0rpx;
		text-align: left;
		font-size: 32rpx;
		color: #999999;
		line-height: 40rpx;
	}

	.bg-confirm {
		background: #3e97b0;
		color: #ffffff;
	}

	.cu-btn {
		flex: 1;
	}

	.cu-modal {
		z-index: 9999;
		text-align: left;
	}

	.container {
		display: flex;
		flex-direction: column;
		height: 88%;
	}

	.scroll-region {
		width: 100%;
		height: 100%;
		/* 设置滚动区域的高度 */
		overflow-y: auto;
		/* 仅允许垂直滚动，也可用scroll-y代替 */
	}

	.cu-avatar {
		background: #fff;
	}

	.cu-item {
		height: 240rpx !important;
	}
</style>